package com.hw.one.dam2.service;

import com.alibaba.fastjson.JSONArray;
import com.hw.one.dam2.dao.IRegionSiteDao;
import com.hw.one.dam2.dao.ISourceDataDao;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * Copyright 2017 汉威.智慧环保事业部.
 * Created by 李亚坤 on 2017/06/13 14:40.
 *
 * 监测设备逻辑接口实现类
 */
@Service
public class MonitorDeviceService implements IMonitorDeviceService {

    @Resource
    private IRegionSiteDao regionSiteDao;

    @Resource
    private ISourceDataDao sourceDataDao;

    @Override
    public Map<String, Object> findDataListBySnCode(String snCode) {

        List<Map> sourceDataList = sourceDataDao.findSourceDataListBySnCode(snCode);
        if (sourceDataList == null || sourceDataList.size() < 1) {
            return new HashMap<>();
        }
        Integer monitorCode = (Integer) sourceDataList.get(0).get("MONITORCODE");
        if ("DQJC123".equals(snCode)) {
            snCode = "B329091010";
        }
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("snCode", snCode);
        // 查询校准变量
        List<Map<String, Object>> monitorSetList = regionSiteDao.findSiteMonitorSetBySnCode(paramMap);
        JSONArray monitorTimeList = new JSONArray();
        JSONArray pm10List = new JSONArray();
        JSONArray pm25List = new JSONArray();
        JSONArray so2List = new JSONArray();
        JSONArray no2List = new JSONArray();
        JSONArray coList = new JSONArray();
        JSONArray o3List = new JSONArray();
        JSONArray tempList = new JSONArray();
        JSONArray humList = new JSONArray();
        Collections.reverse(sourceDataList);
        // 循环数据
        if (257 == monitorCode && null != monitorSetList && !monitorSetList.isEmpty()) {
            // 空气站数据
            for (Map sourceData : sourceDataList) {
                Map<String, Object> monitorData = (Map<String, Object>) sourceData.get("MONITORDATA");
                monitorTimeList.add(sourceData.get("MONITORTIME"));
                pm10List.add(setValue(monitorSetList, "PM10", monitorData.get("PM10")));
                pm25List.add(setValue(monitorSetList, "PM25", monitorData.get("PM25")));
                so2List.add(setValue(monitorSetList, "SO2", monitorData.get("SO2")));
                no2List.add(setValue(monitorSetList, "NO2", monitorData.get("NO2")));
                coList.add(setValue(monitorSetList, "CO", monitorData.get("CO")));
                o3List.add(setValue(monitorSetList, "O3", monitorData.get("O3")));
                tempList.add(monitorData.get("TEMP"));
                humList.add(monitorData.get("HUM"));
            }
        }else {
            for (Map sourceData : sourceDataList) {
                Map<String, Object> monitorData = (Map<String, Object>) sourceData.get("MONITORDATA");
                monitorTimeList.add(sourceData.get("MONITORTIME"));
                pm10List.add(monitorData.get("PM10"));
                pm25List.add(monitorData.get("PM25"));
                so2List.add(monitorData.get("SO2"));
                no2List.add(monitorData.get("NO2"));
                coList.add(monitorData.get("CO"));
                o3List.add(monitorData.get("O3"));
                tempList.add(monitorData.get("TEMP"));
                humList.add(monitorData.get("HUM"));
            }
        }
        JSONArray dataList = new JSONArray();
        JSONArray factorName = new JSONArray();
        factorName.add("PM10");
        factorName.add("PM2.5");
        factorName.add("SO2");
        factorName.add("NO2");
        factorName.add("CO");
        factorName.add("O3");
        factorName.add("温度");
        factorName.add("湿度");
        dataList.add(pm10List);
        dataList.add(pm25List);
        dataList.add(so2List);
        dataList.add(no2List);
        dataList.add(coList);
        dataList.add(o3List);
        dataList.add(tempList);
        dataList.add(humList);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("timeList", monitorTimeList);
        resultMap.put("factorList", factorName);
        resultMap.put("dataList", dataList);
        return resultMap;
    }

    /**
     * 传入浓度和变量值和保留小数位,校准数据
     * @param obj
     * @param k
     * @param b
     * @param scale
     * @return
     */
    private Double toNumber(Object obj, Double k, Double b, int scale) {
        if (null == obj){
            return 0.0;
        }
        Double value = NumberUtils.toDouble(String.valueOf(obj),0);
        if (0 == value) {
            return 0.0;
        }
        Double sum = value * k + b;
        return Double.valueOf(String.valueOf(sum).substring(0, String.valueOf(sum).indexOf(".") + scale));
    }

    /**
     * 根据变量和因子名称和因子原始数据 校准数据
     * @param monitorSetList
     * @param factorName
     * @param sourceObj
     * @return
     */
    private Double setValue(List<Map<String, Object>> monitorSetList, String factorName, Object sourceObj) {
        for (Map<String, Object> monitorSet : monitorSetList) {
            if (factorName.equals(monitorSet.get("itemCode"))) {
                if ("O3".equals(factorName)) {
                    return toNumber(sourceObj, Double.valueOf(String.valueOf(monitorSet.get("regrSlope"))), Double.valueOf(String.valueOf(monitorSet.get("regrInter"))), 3);
                }
                return toNumber(sourceObj, Double.valueOf(String.valueOf(monitorSet.get("regrSlope"))), Double.valueOf(String.valueOf(monitorSet.get("regrInter"))), 0);
            }
        }
        return 0.0;
    }
}
